<?php
/**
 * Created by PhpStorm.
 * User: panchaozhi
 * Date: 2016/11/15
 * Time: 23:58
 */
namespace frontend\controllers;

use Yii;
use yii\web\Controller;
use common\models\Cate;
use frontend\models\Article;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
use common\models\PostExtendsModel;
use common\models\ArticleModel;
use common\models\Comment;
use common\models\Reply;
use common\models\User;
use yii\web\NotFoundHttpException;

class ArticleController extends Controller
{

    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => [
                    'index',
                    'create',
                    'upload',
                    'ueditor'
                ],
                'rules' => [
                    [
                        'actions' => [
                            'index'
                        ],
                        'allow' => true
                    ],
                    // 'roles' => ['?'],
                    [
                        'actions' => [
                            'create',
                            'upload',
                            'ueditor'
                        ],
                        'allow' => true,
                        'roles' => [
                            '@'
                        ]
                    ]
                ]
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    '*' => [
                        'get',
                        'post'
                    ],
                    'create' => [
                        'get',
                        'post'
                    ]
                ]
            ]
        ];
    }

    public function actions()
    {
        return [
            'upload' => [
                'class' => 'common\widgets\file_upload\UploadAction', // 这里扩展地址别写错
                'config' => [
                    'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}"
                ]
            ]
        ];
        
        return [
            'ueditor' => [
                'class' => 'common\widgets\ueditor\UeditorAction',
                'config' => [
                    // 上传图片配置
                    'imageUrlPrefix' => Yii::$app->params['upload_url'], /* 图片访问路径前缀 */
                    'imagePathFormat' => "/image/{yyyy}{mm}{dd}/{time}{rand:6}", /* 上传保存路径,可以自定义保存路径和文件名格式 */
                ]
            ]
        ];
    }

    public function actionIndex()
    {
        return $this->render('index');
    }

    /**
     * 创建文章的操作
     *
     * @return \yii\web\Response|string
     */
    public function actionCreate()
    {
        $model = new Article();
        
        $model->setScenario(Article::SCENARIOS_CREATE);
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            if (! $model->create()) {
                Yii::$app->session->setFlash('warning', $model->_lassError);
            } else {
                return $this->redirect([
                    'article/view',
                    'id' => $model->id
                ]);
            }
        }
        
        $cate = Cate::getAllCate();
        
        return $this->render('create', [
            'model' => $model,
            'cate' => $cate
        ]);
    }

    public function actionView($id)
    {
        $model = new Article();
        $data = $model->getViewById($id);
        
        $model = new PostExtendsModel();
        $model->upCounter([
            'post_id' => $id
        ], 'browser', 1);
        
        $post = ArticleModel::findOne($id);        
        $comments = Comment::findAll([
            'post_id' => $id,
            'status' => 1
        ]);
        $replys = array();
        foreach ($comments as $comment) {
            $replys[$comment->id] = Reply::findAll([
                'comment_id' => $comment->id,
                'status' => 1
            ]);
        }
        
        return $this->render('view', [
            'data' => $data,
            'post' => $post,
            'comments' => $comments,
            'replys' => $replys,
        ]);
    }

    public function actionItem()
    {
        return $this->render('item');
    }
    
    /**
     * Updates an existing ArticleModel model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate($id)
    {
        $cate = Cate::getAllCate();
        
        $model = $this->findModel($id);
    
        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect(['view', 'id' => $model->id]);
        } else {
            return $this->render('update', [
                'model' => $model,
                'cate' => $cate,
            ]);
        }
    }
    
    /**
     * Deletes an existing ArticleModel model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionDelete($id)
    {
        $this->findModel($id)->delete();
    
        return $this->redirect(['index']);
    }
    
    /**
     * Finds the ArticleModel model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ArticleModel the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ArticleModel::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('请求页面没有找到.');
        }
    }
}
